package org.wlgzs.shop.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.wlgzs.shop.bean.*;
import org.wlgzs.shop.dao.GoodsDataMapper;
import org.wlgzs.shop.service.IGoodsDateService;
import org.wlgzs.shop.service.IManagerKind;
import org.wlgzs.shop.service.IStoreService;
import org.wlgzs.shop.utils.ArraysUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;


@Api(tags = "这是的接口", value = "HomeController")
@Controller

public class HomeController {

    @Autowired
    IGoodsDateService goodsDateService;

    @Autowired
    IStoreService storeService;

    @Autowired
    GoodsDataMapper goodsDataMapper;

    @Autowired
    IManagerKind managerKind;


    private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

    /**
     *商品名称搜索
     * @param goodsName
     * @return Msg
     */
    @ResponseBody
    @RequestMapping("/selectGoodsDate")
    @ApiOperation(value ="商品名称搜索",httpMethod = "POST", response =Msg.class)
    @ApiImplicitParam(paramType = "query",name="goodsName",value ="商品信息",dataType = "String")
    public Msg selectGoodsDate(@Param("goodsName") String goodsName) {
//        ModelAndView modelAndView=new ModelAndView();
        if (goodsDateService.goodsNameSelectGoodsDate(goodsName).size() == 0) {
            logger.error("您查询的信息为空，请重新输入");
//            modelAndView.addObject("error", "您查询的信息为空，请重新输入");
//            modelAndView.setViewName("index");
            return Msg.fail().add("error","您查询的信息为空，请重新输入");
        } else {
            List<GoodsData> returnGoodsDateList = goodsDateService.goodsNameSelectGoodsDate(goodsName);
            List<String> returnList=new ArrayList<>();
            Iterator<GoodsData> iteratorGoodsDate = returnGoodsDateList.iterator();
            int a=0;
            while (iteratorGoodsDate.hasNext()) {
                a++;
                if(a==6){
                    break;
                }
                GoodsData returnGoodsDate=iteratorGoodsDate.next();
                logger.info("您查询的信息为" +returnGoodsDate);
                returnList.add(returnGoodsDate.getGoodsName());
            }
//            modelAndView.addObject("goodsDate", goodsDateService.GoodsDateSelectGoods(goodsDate));
//            modelAndView.setViewName("index");
            return Msg.success().add("goodsName", returnList);
        }
    }

    /**
     * 店铺名称搜索
     * @param storeName
     * @return Msg
     */
    @ResponseBody
    @RequestMapping("/selectStore")
    @ApiOperation(value ="店铺名称搜索",httpMethod = "POST", response =Msg.class)
    @ApiImplicitParam(paramType = "query",name="storeName",value ="店铺信息",dataType = "String")
    public Msg selectStore(@Param("storeName")String storeName) {
//        ModelAndView modelAndView=new ModelAndView();
        if (storeService.StoreNameSelectStore(storeName).size() ==0 ) {
            logger.error("您查询的信息为空，请重新输入");
//            modelAndView.addObject("error", "您查询的信息为空，请重新输入");
//            modelAndView.setViewName("index");
            return Msg.fail().add("error", "您查询的信息为空，请重新输入");
        } else {
            List<Store> returnStoreList = storeService.StoreNameSelectStore(storeName);
            List<String> returnList=new ArrayList<>();
            Iterator<Store> iteratorStore = returnStoreList.iterator();
            while (iteratorStore.hasNext()) {
                Store returnStore=iteratorStore.next();
                logger.info("您查询的信息为" + returnStore);
                returnList.add(returnStore.getStoreName());
            }
//            modelAndView.addObject("storeName", storeService.StoreNameSelectStore(storeName));
//            modelAndView.setViewName("index");
            return Msg.success().add("storeName", returnList);
        }
    }


    @ApiOperation(value = "根据一级分类查询二级分类",httpMethod = "POST",response = Msg.class)
    @ResponseBody
    @RequestMapping("/selectTwoKind")
    public Msg showKindTwo(String oneKindId){
        List<GoodsAllKindTwo> list=managerKind.oneContainsTwo(oneKindId);
        if(list!=null){
            return Msg.success().add("kindTwo",list);
        }
        else {
            return Msg.fail().add("error","不包含二级分类");
        }
    }

    @ApiOperation(value = "模糊查询",httpMethod = "POST",response = Msg.class)
    @RequestMapping("/selectDateAll")
    public String selectDate(String date){
        String url=goodsDateService.selectTrueGoods(date);
        logger.info(url);
        return url;
    }


}